<!-- 更新订单模态框 -->
<div id="Order_updateModal" style="display: none;">
    <div style="padding: 20px;">
        <h3 style="text-align: center; margin-bottom: 20px;">订单详情</h3>

        <!-- 基础信息部分 -->
        <div class="layui-row">
            <div class="layui-col-md12">
                <div class="layui-form-item">
                    <label class="layui-form-label">订单ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="order_id" readonly class="layui-input layui-bg-gray">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">代练通ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="dailiantong_order_id" readonly class="layui-input layui-bg-gray">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">代练丸子ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="dailianwangzi_order_id" readonly class="layui-input layui-bg-gray">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">订单名称</label>
                    <div class="layui-input-block">
                        <textarea id="order_name" class="layui-textarea" style="height: 100px;"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">服务器</label>
                    <div class="layui-input-block">
                        <input type="text" id="receiving_server" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">订单状态</label>
                    <div class="layui-input-block">
                        <select id="order_status" class="layui-select">
                            <option value="">请选择状态</option>
                            <option value="1">待接手</option>
                            <option value="2">代练中</option>
                            <option value="3">待验收</option>
                            <option value="4">撤销中</option>
                            <option value="5">撤销完成</option>
                            <option value="6">仲裁中</option>
                            <option value="7">已仲裁</option>
                            <option value="8">锁定中</option>
                            <option value="9">已结算</option>
                            <option value="10">订单异常</option>
                            <option value="11">已取消</option>
                            <option value="12">待发布</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- 号主信息部分 -->
        <div class="layui-row" style="margin-top: 20px;">
            <div class="layui-col-md12">
                <h4 style="padding-left: 15px; margin-bottom: 10px; font-weight: bold; border-left: 3px solid #1E9FFF;">
                    号主信息</h4>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">角色名</label>
                    <div class="layui-input-block">
                        <input type="text" id="character_name" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">号主账号</label>
                    <div class="layui-input-block">
                        <input type="text" id="account_owner" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">号主密码</label>
                    <div class="layui-input-block">
                        <input type="text" id="account_password" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">号主手机</label>
                    <div class="layui-input-block">
                        <input type="text" id="owner_phone" class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <!-- 接单信息部分 -->
        <div class="layui-row" style="margin-top: 20px;">
            <div class="layui-col-md12">
                <h4 style="padding-left: 15px; margin-bottom: 10px; font-weight: bold; border-left: 3px solid #1E9FFF;">
                    接单信息</h4>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">接单价格</label>
                    <div class="layui-input-block">
                        <input type="text" id="receiving_price" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">接单保证金</label>
                    <div class="layui-input-block">
                        <input type="text" id="receiving_margin" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">接单效率费</label>
                    <div class="layui-input-block">
                        <input type="text" id="receiving_efficiency_fee" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">接单时长</label>
                    <div class="layui-input-block">
                        <input type="text" id="receiving_delivery_time" class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <!-- 发布信息部分 -->
        <div class="layui-row" style="margin-top: 20px;">
            <div class="layui-col-md12">
                <h4 style="padding-left: 15px; margin-bottom: 10px; font-weight: bold; border-left: 3px solid #1E9FFF;">
                    发布信息</h4>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">发布价格</label>
                    <div class="layui-input-block">
                        <input type="text" id="posting_price" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">发布安全费</label>
                    <div class="layui-input-block">
                        <input type="text" id="posting_security_fee" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">发布效率费</label>
                    <div class="layui-input-block">
                        <input type="text" id="posting_efficiency_fee" class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <!-- 财务与时间信息 -->
        <div class="layui-row" style="margin-top: 20px;">
            <div class="layui-col-md12">
                <h4 style="padding-left: 15px; margin-bottom: 10px; font-weight: bold; border-left: 3px solid #1E9FFF;">
                    财务与时间信息</h4>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">完成利润</label>
                    <div class="layui-input-block">
                        <input type="text" id="completed_profit" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">未完成利润</label>
                    <div class="layui-input-block">
                        <input type="text" id="uncompleted_profit" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">创建时间</label>
                    <div class="layui-input-block">
                        <input type="text" id="create_time" readonly class="layui-input layui-bg-gray">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">更新时间</label>
                    <div class="layui-input-block">
                        <input type="text" id="update_time" readonly class="layui-input layui-bg-gray">
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 备注信息 -->
        <div class="layui-row" style="margin-top: 20px;">
            <div class="layui-col-md12">
                <h4 style="padding-left: 15px; margin-bottom: 10px; font-weight: bold; border-left: 3px solid #1E9FFF;">
                    备注信息</h4>
            </div>
            <div class="layui-col-md12">
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea id="remark" class="layui-textarea" style="height: 120px;"></textarea>
                    </div>
                </div>
            </div>
        </div>

        <!-- 操作按钮 -->
        <div class="layui-row" style="margin-top: 20px;">
            <div class="layui-col-md12" style="text-align: center;">
                <button id="Order_saveBtn" class="layui-btn layui-btn-normal">保存</button>
                <button id="Order_closeModalBtn" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </div>
</div>

<script>
        // 全局变量，存储模态框实例
    let Order_updateModalInstance = null;
     // 存储原始订单数据，用于比较哪些字段被修改
    let Order_originalData = {};
        /**
     * 保存订单更新
     */
    function Order_saveOrderUpdate() {
        // 获取表单数据
        const formData = {
            order_id: document.getElementById('order_id').value,
            dailiantong_order_id: document.getElementById('dailiantong_order_id').value,
            dailianwangzi_order_id: document.getElementById('dailianwangzi_order_id').value,
            order_name: document.getElementById('order_name').value,
            receiving_server: document.getElementById('receiving_server').value,
            status: document.getElementById('order_status').value,
            // 号主信息
            character_name: document.getElementById('character_name').value,
            account_owner: document.getElementById('account_owner').value,
            account_password: document.getElementById('account_password').value,
            owner_phone: document.getElementById('owner_phone').value,
            // 备注信息
            remark: document.getElementById('remark').value,

            receiving_price: document.getElementById('receiving_price').value,
            receiving_margin: document.getElementById('receiving_margin').value,
            receiving_efficiency_fee: document.getElementById('receiving_efficiency_fee').value,
            receiving_delivery_time: document.getElementById('receiving_delivery_time').value,
            posting_price: document.getElementById('posting_price').value,
            posting_security_fee: document.getElementById('posting_security_fee').value,
            posting_efficiency_fee: document.getElementById('posting_efficiency_fee').value,
            completed_profit: document.getElementById('completed_profit').value,
            uncompleted_profit: document.getElementById('uncompleted_profit').value
        };

        // 创建仅包含修改字段的对象
        const updateData = {};

        // 检查哪些字段被修改
        if (formData.order_id !== Order_originalData.order_id) updateData.order_id = formData.order_id;
        if (formData.dailiantong_order_id !== Order_originalData.dailiantong_order_id) updateData.dailiantong_order_id = formData.dailiantong_order_id;
        if (formData.dailianwangzi_order_id !== Order_originalData.dailianwangzi_order_id) updateData.dailianwangzi_order_id = formData.dailianwangzi_order_id;
        if (formData.order_name !== Order_originalData.order_name) updateData.order_name = formData.order_name;
        if (formData.receiving_server !== Order_originalData.receiving_server) updateData.receiving_server = formData.receiving_server;
        if (formData.status !== (Order_originalData.status ? Order_originalData.status.code : '')) updateData.status = formData.status;

        // 检查号主信息字段修改
        if (formData.character_name !== Order_originalData.character_name) updateData.character_name = formData.character_name;
        if (formData.account_owner !== Order_originalData.account_owner) updateData.account_owner = formData.account_owner;
        if (formData.account_password !== Order_originalData.account_password) updateData.account_password = formData.account_password;
        if (formData.owner_phone !== Order_originalData.owner_phone) updateData.owner_phone = formData.owner_phone;
        
        // 检查备注字段修改
        if (formData.remark !== Order_originalData.remark) updateData.remark = formData.remark;

        if (formData.receiving_price !== Order_originalData.receiving_price) updateData.receiving_price = formData.receiving_price;
        if (formData.receiving_margin !== Order_originalData.receiving_margin) updateData.receiving_margin = formData.receiving_margin;
        if (formData.receiving_efficiency_fee !== Order_originalData.receiving_efficiency_fee) updateData.receiving_efficiency_fee = formData.receiving_efficiency_fee;
        // 移除小时后缀进行比较
        const originalDeliveryTime = (Order_originalData.receiving_delivery_time || '').toString().replace(' 小时', '');
        if (formData.receiving_delivery_time !== originalDeliveryTime) updateData.receiving_delivery_time = formData.receiving_delivery_time;
        if (formData.posting_price !== Order_originalData.posting_price) updateData.posting_price = formData.posting_price;
        if (formData.posting_security_fee !== Order_originalData.posting_security_fee) updateData.posting_security_fee = formData.posting_security_fee;
        if (formData.posting_efficiency_fee !== Order_originalData.posting_efficiency_fee) updateData.posting_efficiency_fee = formData.posting_efficiency_fee;
        if (formData.completed_profit !== Order_originalData.completed_profit) updateData.completed_profit = formData.completed_profit;
        if (formData.uncompleted_profit !== Order_originalData.uncompleted_profit) updateData.uncompleted_profit = formData.uncompleted_profit;

        // 如果没有修改任何字段
        if (Object.keys(updateData).length === 0) {
            layer.msg('没有修改任何字段', { icon: 3 });
            return;
        }
        updateData.order_id = formData.order_id


        // 发送更新请求
        axios.post('/api/order_v2/update/', updateData)
            .then(function (response) {
                if (response.data && response.data.code === 0) {
                    layer.msg('订单更新成功', { icon: 1 });
                    // 关闭模态框
                    if (Order_updateModalInstance !== null) {
                        layer.close(Order_updateModalInstance);
                        Order_updateModalInstance = null;
                    }
                    // 刷新表格数据 - 支持多种场景
                    // 1. 尝试刷新父页面表格
                    if (window.parent && window.parent.refreshOrderTable) {
                        window.parent.refreshOrderTable();
                    }
                    // 2. 尝试直接重载当前页面的layui表格
                    else if (window.BS_Layui_table) {
                        // 查找页面中的订单表格实例并刷新
                        try {
                            window.BS_Layui_table.reload('OrderListTable');
                        } catch (e) {
                            console.log('尝试直接重载表格失败:', e);
                        }
                    }
                    // 3. 如果是在iframe中，通知父页面更新
                    else if (window.parent && window.parent.layui && window.parent.layui.table) {
                        try {
                            window.parent.layui.table.reload('OrderListTable');
                        } catch (e) {
                            console.log('尝试在父页面重载表格失败:', e);
                        }
                    }
                } else {
                    layer.msg('更新失败：' + (response.data ? response.data.message : '未知错误'), { icon: 2 });
                }
            })
            .catch(function (error) {
                layer.msg('网络错误，请稍后重试', { icon: 2 });
                console.error('更新订单失败:', error);
            });
    }

    // 保存按钮点击事件
    document.querySelector('#Order_saveBtn').addEventListener('click', function () {
        Order_saveOrderUpdate();
    });

    // 关闭模态框按钮点击事件
    document.querySelector('#Order_closeModalBtn').addEventListener('click', function () {
        if (Order_updateModalInstance !== null) {
            layer.close(Order_updateModalInstance);
            Order_updateModalInstance = null;
        }
    });

        /**
     * 显示更新订单模态框
     * @param {Object} orderData - 订单数据
     */
    function Order_showUpdateModal(orderData) {
        // 关闭已存在的模态框
        if (Order_updateModalInstance !== null) {
            layer.close(Order_updateModalInstance);
        }

        // 深拷贝原始数据，用于比较
        Order_originalData = JSON.parse(JSON.stringify(orderData));

        // 填充订单数据到表单
        document.getElementById('order_id').value = orderData.order_id || '';
        document.getElementById('dailiantong_order_id').value = orderData.dailiantong_order_id || '';
        document.getElementById('dailianwangzi_order_id').value = orderData.dailianwangzi_order_id || '';
        document.getElementById('order_name').value = orderData.order_name || '';
        document.getElementById('receiving_server').value = orderData.receiving_server || '';
        // 设置状态选择器的值
        const statusSelect = document.getElementById('order_status');
        if (orderData.status && orderData.status.code) {
            statusSelect.value = orderData.status.code;
        } else {
            statusSelect.value = '';
        }
        // 填充号主信息
        document.getElementById('character_name').value = orderData.character_name || '';
        document.getElementById('account_owner').value = orderData.account_owner || '';
        document.getElementById('account_password').value = orderData.account_password || '';
        document.getElementById('owner_phone').value = orderData.owner_phone || '';

        document.getElementById('receiving_price').value = orderData.receiving_price || '';
        document.getElementById('receiving_margin').value = orderData.receiving_margin || '';
        document.getElementById('receiving_efficiency_fee').value = orderData.receiving_efficiency_fee || '';
        // 移除小时后缀以支持编辑
        const deliveryTime = orderData.receiving_delivery_time || '';
        document.getElementById('receiving_delivery_time').value = deliveryTime.toString().replace(' 小时', '');
        document.getElementById('posting_price').value = orderData.posting_price || '';
        document.getElementById('create_time').value = orderData.create_time || '';
        document.getElementById('posting_security_fee').value = orderData.posting_security_fee || '';
        document.getElementById('posting_efficiency_fee').value = orderData.posting_efficiency_fee || '';
        document.getElementById('completed_profit').value = orderData.completed_profit || '';
        document.getElementById('uncompleted_profit').value = orderData.uncompleted_profit || '';
        document.getElementById('update_time').value = orderData.update_time || '';
        // 填充备注信息
        document.getElementById('remark').value = orderData.remark || '';

        // 初始化layui表单组件
        window.BS_Layui_form.render('select');

        // 打开新的模态框
        Order_updateModalInstance = layer.open({
            type: 1,
            title: '更新订单',
            content: window.BS_Layui_$('#Order_updateModal'),
            area: ['900px', '600px'],
            shade: 0.6,
            btn: [], // 不显示默认按钮
            cancel: function () {
                Order_updateModalInstance = null;
            }
        });
    }

</script>